package sketch.seg.cd.exp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class DataAnalyzer {

	private final int NUM = 20;

	private final int USER = 7;

	private int LINE[] = new int[] {
		2,5,7,9,11,12,14,18,19,20
	};

	private int HYBRID[] = new int[] {
		1,3,4,6,8,10,13,15,16,17
	};

	private int LL = 0, HH = 0;

	private int LLS = 0, LLN = 0, HHS = 0, HHN = 0;

	private double line_pre, line_rec, hybrid_pre, hybrid_rec;

	private double L_PRE[], L_REC[], H_PRE[], H_REC[];

	private BufferedReader reader;

	private int M[] = new int[NUM];

	private int N1[][], N2[][];

	private int S1[][], S2[][];

	private String name[];

	private int SS1[], SS2[], NN1[], NN2[];

	private int TS1[], TN1[], TS2[], TN2[];

	private double pre1[], rec1[], pre2[], rec2[];

	private int MM = 0;

	public DataAnalyzer(String filename) {

		L_REC = new double[USER];
		L_PRE = new double[USER];
		H_REC = new double[USER];
		H_PRE = new double[USER];

		TS1 = new int[NUM];
		TN1 = new int[NUM];
		TS2 = new int[NUM];
		TN2 = new int[NUM];

		SS1 = new int[USER];
		SS2 = new int[USER];
		NN1 = new int[USER];
		NN2 = new int[USER];

		pre1 = new double[USER];
		rec1 = new double[USER];
		pre2 = new double[USER];
		rec2 = new double[USER];

		N1 = new int[USER][];
		N2 = new int[USER][];
		S1 = new int[USER][];
		S2 = new int[USER][];
		name = new String[USER];

		for (int i = 0; i < USER; i++) {
			N1[i] = new int[NUM];
			N2[i] = new int[NUM];
			S1[i] = new int[NUM];
			S2[i] = new int[NUM];

			SS1[i] = 0;
			SS2[i] = 0;
			NN1[i] = 0;
			NN2[i] = 0;
		}

		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filename))));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void analyze() {
		try {
			String line = "";
			String prev = "";
			int i = -1;
			boolean single = false;

			while ((line = reader.readLine()) != null) {

				if (line.length() < 2)
					continue;

				char pre1 = line.charAt(0);

				switch (pre1) {
				case '-':
					i++;
					name[i] = prev;
					single = true;
					break;
				case 'n':
					if (single) {
						parseArray(N1[i], line);
						single = false;
					} else {
						parseArray(N2[i], line);
					}
					break;
				case 's':
					if (line.charAt(1) == 'i')
						break;
					if (single) {
						parseArray(S1[i], line);
					} else {
						parseArray(S2[i], line);
//						single = true;
					}
					break;
				case 'm':
					parseArray(M, line);
				default:
					prev = line;
					break;
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		for (int i = 0; i < NUM; i++) {
			MM += M[i];
			for (int j = 0; j < USER; j++) {
				SS1[j] += S1[j][i];
				SS2[j] += S2[j][i];
				NN1[j] += N1[j][i];
				NN2[j] += N2[j][i];

				TS1[i] += S1[j][i];
				TS2[i] += S2[j][i];
				TN1[i] += N1[j][i];
				TN2[i] += N2[j][i];

			}
		}

		for (int i = 0; i < USER; i++) {
			pre1[i] = divide(SS1[i], NN1[i]);
			rec1[i] = divide(SS1[i], MM);
			pre2[i] = divide(SS2[i], NN2[i]);
			rec2[i] = divide(SS2[i], MM);
		}

		int l_s[] = new int[USER],
			l_n[] = new int[USER],
			h_s[] = new int[USER],
			h_n[] = new int[USER];

		for (int i = 0; i < LINE.length; i++) {
			int ii = LINE[i] - 1;
			LL += M[ii];

			for (int j = 0; j < USER; j++) {
				LLS += S2[j][ii];
				LLN += N2[j][ii];

				l_s[j] += S2[j][ii];
				l_n[j] += N2[j][ii];
			}
		}

		for (int i = 0; i < HYBRID.length; i++) {
			int ii = HYBRID[i] - 1;
			HH += M[ii];

			for (int j = 0; j < USER; j++) {
				HHS += S2[j][ii];
				HHN += N2[j][ii];

				h_s[j] += S2[j][ii];
				h_n[j] += N2[j][ii];
			}
		}

		for (int i = 0; i < USER; i++) {
			L_REC[i] = divide(l_s[i], LL);
			L_PRE[i] = divide(l_s[i], l_n[i]);
			H_REC[i] = divide(h_s[i], HH);
			H_PRE[i] = divide(h_s[i], h_n[i]);
		}

		line_rec = divide(LLS, LL * USER);
		line_pre = divide(LLS, LLN);
		hybrid_rec = divide(HHS, HH * USER);
		hybrid_pre = divide(HHS, HHN);

//		printData();
//		printResult();
	}

	private void printResult() {
		System.out.println("Statistic results ==========================");

		for (int i = 0; i < USER; i++) {
			System.out.println("User: " + name[i] + " ---");

			System.out.println("\t single: pre=" + pre1[i] + ", rec=" + rec1[i]);
			System.out.println("\t double: pre=" + pre2[i] + ", rec=" + rec2[i]);
		}
	}

	private double divide(int a, int b) {
		// a / b
		return (double)a / (double)b;
	}

	private void printData() {
		System.out.println("Mark --------------------");
		System.out.println("\tM: " + printArray(M) + " = " + MM);

		for (int i = 0; i < USER; i ++) {
			System.out.println("User: " + name[i] + " --------");
			System.out.println("\tS1: " + printArray(S1[i]) + " = " + SS1[i]);
			System.out.println("\tN1: " + printArray(N1[i]) + " = " + NN1[i]);
			System.out.println("\tS2: " + printArray(S2[i]) + " = " + SS2[i]);
			System.out.println("\tN2: " + printArray(N2[i]) + " = " + NN2[i]);
		}


	}

	public void printData2() {
		for (int i = 0; i < USER; i++) {
			System.out.println(name[i]);

			System.out.println("\tline-type pre=" + L_PRE[i] +
			                              " rec=" + L_REC[i]);
			System.out.println("\thybrid-type pre=" + H_PRE[i] +
					                        " rec=" + H_REC[i]);
		}
	}

	private String printArray(int[] array) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < array.length; i++) {
			sb.append(array[i]);
			sb.append(" ");
		}
		return sb.toString();
	}

	private void parseArray(int[] array, String s) {
		s = s.replaceAll("[ \t\n\f\r]+", ":");
		String[] ss = s.split(":");

		for (int i = 1; i < NUM + 1; i++) {
			array[i-1] = Integer.parseInt(ss[i]);
		}
	}

	public void writeResultToFile(String filename) {
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(filename))));

			bw.write("# generated by DataAnalyzer (zhengwt)\n");
			bw.write("# user-index | single-pre | single-rec | double-pre | double-rec\n\n");

			for (int i = 0; i < USER; i++) {
				bw.write("# user: ");
				bw.write(name[i]);
				bw.write("\n");

				bw.write("user" + Integer.toString((i+1)));
				bw.write("\t");
				bw.write(Double.toString(pre1[i]));
				bw.write("\t");
				bw.write(Double.toString(rec1[i]));
				bw.write("\t");
				bw.write(Double.toString(pre2[i]));
				bw.write("\t");
				bw.write(Double.toString(rec2[i]));
				bw.write("\n\n");
			}

			bw.write("\n# End of file");
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		DataAnalyzer da = new DataAnalyzer("C:/Workspace/TeX/MS-Thesis/version.0.1/seg.data");
		da.analyze();
		da.printData2();
//		da.writeResultToFile("C:/Workspace/Exp/segmentation/figure/seg.result.dat");
//		da.writeResult2ToFile("C:/Workspace/Exp/segmentation/figure/seg.result2.dat");
	}

	public void writeResult2ToFile(String filename) {
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(filename))));

			bw.write("# generated by DataAnalyzer (zhengwt)\n");
			bw.write("# line-type | double-pre | double-rec\n\n");

			bw.write("line-stroke\t");
			bw.write(Double.toString(line_pre) + "\t");
			bw.write(Double.toString(line_rec) + "\n");
			bw.write("hybrid-stroke\t");
			bw.write(Double.toString(hybrid_pre) + "\t");
			bw.write(Double.toString(hybrid_rec) + "\n");

			bw.write("\n# end of file");
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
